package io.renren.modules.grant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.grant.dao.NoticeListDao;
import io.renren.modules.grant.dto.NoticeListDTO;
import io.renren.modules.grant.entity.NoticeListEntity;
import io.renren.modules.grant.service.NoticeListService;
import io.renren.modules.grant.service.NoticeTypeService;
import io.renren.modules.grant.vo.NoticeListVO;
import io.renren.modules.grant.vo.NoticeTypeVO;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 公告内容管理
 */
@Service
public class NoticeListServiceImpl extends CrudServiceImpl<NoticeListDao, NoticeListEntity, NoticeListVO> implements NoticeListService {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private NoticeTypeService noticeTypeService;

    @Override
    public QueryWrapper<NoticeListEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");
        String title = (String) params.get("title");
        String noticeTypeId = (String) params.get("noticeTypeId");

        QueryWrapper<NoticeListEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.eq(StringUtils.isNotBlank(noticeTypeId), "notice_type_id", noticeTypeId);
        wrapper.like(StringUtils.isNotBlank(title), "title", title);

        return wrapper;
    }

    @Override
    public void save(NoticeListDTO dto) {
        NoticeListEntity noticeListEntity = ConvertUtils.sourceToTarget(dto, NoticeListEntity.class);
        this.insert(noticeListEntity);
    }

    @Override
    public void update(NoticeListDTO dto) {
        NoticeListEntity noticeListEntity = ConvertUtils.sourceToTarget(dto, NoticeListEntity.class);
        this.updateById(noticeListEntity);
    }

    @Override
    public PageData<NoticeListVO> noticeListHomePage(Map<String, Object> params) {
        List<SysUserDTO> userDtoList = sysUserService.getAll();
        List<NoticeTypeVO> noticeTypeDtoList = noticeTypeService.getAll();

        QueryWrapper<NoticeListEntity> wrapper = this.getWrapper(params);
        wrapper.eq("status", 1);
        IPage<NoticeListEntity> page = baseDao.selectPage(getPage(params, Constant.CREATE_DATE, false), wrapper);
        PageData<NoticeListVO> pageData = getPageData(page, NoticeListVO.class);
        pageData.getList().forEach(e -> {
            NoticeTypeVO noticeTypeVo = noticeTypeDtoList.stream().filter(j -> j.getId().compareTo(e.getNoticeTypeId()) == 0).findAny().orElse(null);
            e.setNoticeTypeName(noticeTypeVo == null ? "-" : noticeTypeVo.getTypeName());
            SysUserDTO creator = userDtoList.stream().filter(j -> j.getId().compareTo(e.getCreator()) == 0).findAny().orElse(null);
            e.setCreatorName(creator == null ? "-" : creator.getRealName());
            SysUserDTO updater = userDtoList.stream().filter(j -> j.getId().compareTo(e.getUpdater()) == 0).findAny().orElse(null);
            e.setUpdaterName(updater == null ? "-" : updater.getRealName());
        });
        return pageData;
    }

    @Override
    public PageData<NoticeListVO> noticeListPage(Map<String, Object> params) {
        List<SysUserDTO> userDtoList = sysUserService.getAll();
        List<NoticeTypeVO> noticeTypeDtoList = noticeTypeService.getAll();
        PageData<NoticeListVO> page = this.page(params);
        page.getList().forEach(e -> {
            NoticeTypeVO noticeTypeVo = noticeTypeDtoList.stream().filter(j -> j.getId().compareTo(e.getNoticeTypeId()) == 0).findAny().orElse(null);
            e.setNoticeTypeName(noticeTypeVo == null ? "-" : noticeTypeVo.getTypeName());
            SysUserDTO creator = userDtoList.stream().filter(j -> j.getId().compareTo(e.getCreator()) == 0).findAny().orElse(null);
            e.setCreatorName(creator == null ? "-" : creator.getRealName());
            SysUserDTO updater = userDtoList.stream().filter(j -> j.getId().compareTo(e.getUpdater()) == 0).findAny().orElse(null);
            e.setUpdaterName(updater == null ? "-" : updater.getRealName());
        });
        return page;
    }

}
